*************************************************
*                REPLICATION CODE               *
*                                               *
*  Stata code below is to generate results used *
*    in figures 3 & 4 and supplemental tables   *
*     in Desai and Shambaugh, "Measuring the    *
*    global impact of destructive and illegal   *
*          fishing on maritime piracy:          *
*              A spatial analysis               *
*                                               *
*         WARNING:  spatial analyses are        *    
*           computationally intensive           *
*                                               *
*************************************************


*SET APPROPRIATE FILE PATH

cd "USER FILE PATH HERE"



*Open and run "Spatial Setup.do" ONE TIME ONLY 
*to generate and save matrices for later use

use "Piracy Replication Data.dta", clear

do "Replication Setup.do"

set more off
set seed 23423


*************************************
***           Table 1             ***
*************************************

foreach y in 2005 2010 {
	getisord pirates if year_bin==`y', lat(cent_lat) lon(cent_long) swm(pow 1) dist(20036) dunit(km) constant(1) approx largesize
	rename go_z_pirates_p z_piratesINFn_`y'
	rename go_p_pirates_p p_piratesINFn_`y'
}

sort fao_area

by fao_area: sum z_piratesINFn005 if p_piratesINFn005 < 0.05
by fao_area: sum z_piratesINFn010 if p_piratesINFn010 < 0.05



*************************************
***           Figure 3            ***
*************************************

spmatrix clear
spmatrix use W250n using W250n.stswm

spxtregress pirates zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area* y_* if to_use_panel == 1 & to_keep3 == 1, re dvarlag(W250n) force nolog
estat impact zfragile zports zships zmilbase zdiffcatch if e(sample) & ocean==1
estat impact zevap zpop zports if e(sample) & landed==1

spxtregress pirates zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.ocean zports zships zmilbase zdiffdem zdiffpel _fao_area* y_* if to_use_panel == 1 & to_keep3 == 1, re dvarlag(W250n) force nolog
estat impact zdiffdem zdiffpel if e(sample) & ocean==1

spxtregress pirates zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.ocean zports zships zmilbase zdiffcatch _fao_area* y_* if to_use_panel == 1 & to_keep3 == 1 & country_eez==1, re dvarlag(W250n) force nolog
estat impact zfragile zlights zships zmilbase zdiffcatch if e(sample) & ocean==1
estat impact zevap zpop zports if e(sample) & landed==1

spxtregress pirates zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.ocean zports zships zmilbase zdiffpel zdiffdem _fao_area* y_* if to_use_panel == 1 & to_keep3 == 1 & country_eez==1, re dvarlag(W250n) force nolog
estat impact zdiffpel zdiffdem if e(sample) & ocean==1


*************************************
***           Figure 4            ***
*************************************

spmatrix clear
spmatrix use W250n using W250n.stswm

spivregress pirates fragile c.ln_nl_mean##i.landed c.evap_min##i.landed c.ln_pop##i.landed ports ln_ship_mean milbase_total _fao_area* (ln_iuu_tot = ln_npp_tot) if to_use_panel == 1 & to_keep3 == 1 & year_bin==2005, dvarlag(W250n) force nolog impower(1)
est store sar_2005_fao_im1

spivregress pirates fragile c.ln_nl_mean##i.landed c.evap_min##i.landed c.ln_pop##i.landed ports ln_ship_mean milbase_total _fao_area* (ln_iuu_tot = ln_npp_tot) if to_use_panel == 1 & to_keep3 == 1 & year_bin==2010, dvarlag(W250n) force nolog impower(1)
est store sar_2010_fao_im1

spivregress pirates fragile c.ln_nl_mean##i.landed c.evap_min##i.landed c.ln_pop##i.landed ports ln_ship_mean milbase_total _fao_area* (ln_iuu_tot = ln_npp_tot) if to_use_panel == 1 & to_keep3 == 1 & year_bin==2005 & country_eez==1, dvarlag(W250n_eez) force nolog impower(1)
est store sar_2005eez_fao_im1

spivregress pirates fragile c.ln_nl_mean##i.landed c.evap_min##i.landed c.ln_pop##i.landed ports ln_ship_mean milbase_total _fao_area* (ln_iuu_tot = ln_npp_tot) if to_use_panel == 1 & to_keep3 == 1 & year_bin==2010 & country_eez==1, dvarlag(W250n_eez) force nolog impower(1)
est store sar_2010eez_fao_im1


*MARGINAL AND AVERAGE EFFECTS - OCEANIC ONLY*
*
*This module generates point-marginal effects at increments
*from min to max as well as Average marginal effects


centile ln_iuu_tot if to_use_panel == 1 & to_keep3 == 1 & year_bin==2005 & ocean==1, centile(0(10)90 99 100)

est restore sar_2005_fao_im1
margins, at(ln_iuu_tot = (0 0.0004024 0.0010274 0.002637 0.0062964 0.0148493 0.02669 0.0683386 0.2028 0.8655194 3.00444 7.678571))

margins, dydx(ln_iuu_tot) at(ln_iuu_tot = (0.002637))

centile ln_iuu_tot if to_use_panel == 1 & to_keep3 == 1 & year_bin==2010 & ocean==1, centile(0(10)90 99 100)

est restore sar_2010_fao_im1
margins, at(ln_iuu_tot = (0 0.0007597 0.0019668 0.0047536 0.0111662 0.0247498 0.0515434 0.1257953 0.3024292 1.160071 3.52395 8.383233))

margins, dydx(ln_iuu_tot) at(ln_iuu_tot = (0.0047536))


centile ln_iuu_tot if to_use_panel == 1 & to_keep3 == 1 & year_bin==2005 & country_eez==1 & ocean==1, centile(0(10)90 99 100)

est restore sar_2005eez_fao_im1
margins, at(ln_iuu_tot = (0 0.000517 0.0025446 0.0078076 0.0226773 0.0616942 0.1682763 0.4006783 0.9394394 1.703636 3.3813 7.678571))

margins, dydx(ln_iuu_tot) at(ln_iuu_tot = (0.0078076))

centile ln_iuu_tot if to_use_panel == 1 & to_keep3 == 1 & year_bin==2010 & country_eez==1 & ocean==1, centile(0(10)90 99 100)

est restore sar_2010eez_fao_im1
margins, at(ln_iuu_tot = (0 0.0008189 0.0041718 0.0133567 0.0370814 0.1079923 0.2518309 0.5749427 1.258067 2.188916 3.905861 8.383233))

margins, dydx(ln_iuu_tot) at(ln_iuu_tot = (0.0133567))



*************************************
***         SI Table 1            ***
*************************************

*Designate core sample with OLS panel random-effects regression to save time*
xtreg pirates zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area* y_* if to_use_panel == 1 & to_keep3 == 1, re
gen core = e(sample)

*Generate summary statistics from core sample*
xtsum pirates fragile ln_nl_mean evap_min ln_pop ports ln_ship_mean milbase_total ln_diffcatch ln_diffdem ln_diffpel ln_iuu_tot ln_npp_tot if core==1



*************************************
***         SI Table 2            ***
*************************************

spmatrix clear
spmatrix use W250n using W250n.stswm

eststo clear 

log using "Output Data/SI Table 2.smcl"


*SDM
spxtregress pirates zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area* y_* if to_use_panel == 1 & to_keep3 == 1, re dvarlag(W250n) ivarlag(W250n: zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area*) force nolog
est store sdm_250_full
eststo, title("SDM250n; Panel; FAO RE") 

*SDEM
spxtregress pirates zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area* y_* if to_use_panel == 1 & to_keep3 == 1, re dvarlag(W250n) errorlag(W250n) ivarlag(W250n: zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area*) force nolog
est store sdem_250_full
eststo, title("SDEM250n; Panel; FAO RE") 

*SAR
spxtregress pirates zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area* y_* if to_use_panel == 1 & to_keep3 == 1, re dvarlag(W250n) force nolog
est store sar_250_full
eststo, title("SAR250n; Panel; FAO RE") 

*SDM
spxtregress pirates zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffdem zdiffpel _fao_area* y_* if to_use_panel == 1 & to_keep3 == 1, re dvarlag(W250n) ivarlag(W250n: zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffdem zdiffpel _fao_area*) force nolog
est store sdm_250_full
eststo, title("SDM250n; Panel; FAO RE") 

*SDEM
spxtregress pirates zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffdem zdiffpel _fao_area* y_* if to_use_panel == 1 & to_keep3 == 1, re dvarlag(W250n) errorlag(W250n) ivarlag(W250n: zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffdem zdiffpel _fao_area*) force nolog
est store sdem_250_full
eststo, title("SDEM250n; Panel; FAO RE") 

*SAR
spxtregress pirates zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffdem zdiffpel _fao_area* y_* if to_use_panel == 1 & to_keep3 == 1, re dvarlag(W250n) force nolog
est store sar_250_full
eststo, title("SAR250n; Panel; FAO RE") 

esttab using "Tables/Table SI 2.csv", append cells(b(star fmt(3)) se(fmt(3))) mlabels(, titles) stats(N) parentheses

log close


*************************************
***         SI Table 3            ***
*************************************

eststo clear
est clear

log using "Output Data/SI Table 3.smcl", replace

* 500km

spmatrix clear
spmatrix use W500n using W500n.stswm


*SDM
spxtregress pirates zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area* y_* if to_use_panel == 1 & to_keep3 == 1, re dvarlag(W500n) ivarlag(W500n: zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area*) force nolog
est store sdm_500_full
eststo, title("SDM500n; Panel; FAO RE") 

*SDEM
spxtregress pirates zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area* y_* if to_use_panel == 1 & to_keep3 == 1, re dvarlag(W500n) errorlag(W500n) ivarlag(W500n: zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area*) force nolog
est store sdem_500_full
eststo, title("SDEM500n; Panel; FAO RE") 

*SAR
spxtregress pirates zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area* y_* if to_use_panel == 1 & to_keep3 == 1, re dvarlag(W500n) force nolog
est store sar_500_full
eststo, title("SAR500n; Panel; FAO RE") 


* 1000km

spmatrix clear
spmatrix use W1000n using W1000n.stswm

*SDM
spxtregress pirates zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area* y_* if to_use_panel == 1 & to_keep3 == 1, re dvarlag(W1000n) ivarlag(W1000n: zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area*) force nolog
est store sdm_1000_full
eststo, title("SDM1000n; Panel; FAO RE") 

*SDEM
spxtregress pirates zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area* y_* if to_use_panel == 1 & to_keep3 == 1, re dvarlag(W1000n) errorlag(W1000n) ivarlag(W1000n: zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area*) force nolog
est store sdem_1000_full
eststo, title("SDEM1000n; Panel; FAO RE") 

*SAR
spxtregress pirates zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area* y_* if to_use_panel == 1 & to_keep3 == 1, re dvarlag(W1000n) force nolog
est store sar_1000_full
eststo, title("SAR1000n; Panel; FAO RE") 

esttab using "Tables/Table SI 3.csv", append cells(b(star fmt(3)) se(fmt(3))) mlabels(, titles) stats(N) parentheses

log close


*************************************
***         SI Table 4            ***
*************************************

eststo clear
est clear

*SDM
spxtregress pirates zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area* y_* if to_use_panel == 1 & to_keep3 == 1 & country_eez==1, re dvarlag(W250n) ivarlag(W250n: zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area*) force nolog
est store sdm_250_eez
eststo, title("SDM250n_eez; Panel; FAO RE") 

*SDEM
spxtregress pirates zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area* y_* if to_use_panel == 1 & to_keep3 == 1 & country_eez==1, re dvarlag(W250n) errorlag(W250n) ivarlag(W250n: zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area*) force nolog
est store sdem_250_eez
eststo, title("SDEM250n_eez; Panel; FAO RE") 

*SAR
spxtregress pirates zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area* y_* if to_use_panel == 1 & to_keep3 == 1 & country_eez==1, re dvarlag(W250n) force nolog
est store sar_250_eez
eststo, title("SAR250n_eez; Panel; FAO RE") 

*SDM
spxtregress pirates zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffdem zdiffpel _fao_area* y_* if to_use_panel == 1 & to_keep3 == 1 & country_eez==1, re dvarlag(W250n) ivarlag(W250n: zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffdem zdiffpel _fao_area*) force nolog
est store sdm_250_eez
eststo, title("SDM250n_eez; Panel; FAO RE") 

*SDEM
spxtregress pirates zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffdem zdiffpel _fao_area* y_* if to_use_panel == 1 & to_keep3 == 1 & country_eez==1, re dvarlag(W250n) errorlag(W250n) ivarlag(W250n: zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffdem zdiffpel _fao_area*) force nolog
est store sdem_250_eez
eststo, title("SDEM250n_eez; Panel; FAO RE") 

*SAR
spxtregress pirates zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffdem zdiffpel _fao_area* y_* if to_use_panel == 1 & to_keep3 == 1 & country_eez==1, re dvarlag(W250n) force nolog
est store sar_250_eez
eststo, title("SAR250n_eez; Panel; FAO RE") 

esttab using "Tables/Table SI 4.csv", cells(b(star fmt(3)) se(fmt(3))) mlabels(, titles) stats(N) parentheses


*************************************
***         SI Table 5            ***
*************************************

spmatrix clear
spmatrix use W500n using W500n.stswm

eststo clear
est clear


*SDM
spxtregress pirates zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area* y_* if to_use_panel == 1 & to_keep3 == 1 & country_eez==1, re dvarlag(W500n) ivarlag(W500n: zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area*) force nolog
est store sdm_500_eez
eststo, title("SDM500n_eez; Panel; FAO RE") 

*SDEM
spxtregress pirates zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area* y_* if to_use_panel == 1 & to_keep3 == 1 & country_eez==1, re dvarlag(W500n) errorlag(W500n) ivarlag(W500n: zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area*) force nolog
est store sdem_500_eez
eststo, title("SDEM500n_eez; Panel; FAO RE") 

*SAR
spxtregress pirates zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area* y_* if to_use_panel == 1 & to_keep3 == 1 & country_eez==1, re dvarlag(W500n) force nolog
est store sar_500_eez
eststo, title("SAR500n_eez; Panel; FAO RE") 


spmatrix clear
spmatrix use W1000n using W1000n.stswm

*SDM
spxtregress pirates zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area* y_* if to_use_panel == 1 & to_keep3 == 1 & country_eez==1, re dvarlag(W1000n) ivarlag(W1000n: zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area*) force nolog
est store sdm_1000_full
eststo, title("SDM1000n_eez; Panel; FAO RE") 

*SDEM
spxtregress pirates zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area* y_* if to_use_panel == 1 & to_keep3 == 1 & country_eez==1, re dvarlag(W1000n) errorlag(W1000n) ivarlag(W1000n: zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area*) force nolog
est store sdem_1000_full
eststo, title("SDEM1000n_eez; Panel; FAO RE") 

*SAR
spxtregress pirates zfragile c.zlights##i.landed c.zevap##i.landed c.zpop##i.landed zports zships zmilbase zdiffcatch _fao_area* y_* if to_use_panel == 1 & to_keep3 == 1 & country_eez==1, re dvarlag(W1000n) force nolog
est store sar_1000_full
eststo, title("SAR1000n_eez; Panel; FAO RE") 

esttab using "Tables/Table SI 5.csv", cells(b(star fmt(3)) se(fmt(3))) mlabels(, titles) stats(N) parentheses



*************************************
***         SI Table 6            ***
*************************************

spmatrix clear
spmatrix use W250n using W250n.stswm

eststo clear
est clear

spivregress pirates fragile c.ln_nl_mean##i.landed c.evap_min##i.landed c.ln_pop##i.landed ports ln_ship_mean milbase_total _fao_area* (ln_iuu_tot = ln_npp_tot) if to_use_panel == 1 & to_keep3 == 1 & year_bin==2005, dvarlag(W250n) force nolog impower(1)
est store sar_2005_fao_im1
eststo, title("IV; 2005; full sample") 

spivregress pirates fragile c.ln_nl_mean##i.landed c.evap_min##i.landed c.ln_pop##i.landed ports ln_ship_mean milbase_total _fao_area* (ln_iuu_tot = ln_npp_tot) if to_use_panel == 1 & to_keep3 == 1 & year_bin==2010, dvarlag(W250n force nolog impower(1)
est store sar_2010_fao_im1
eststo, title("IV; 2010; full sample") 

spivregress pirates fragile c.ln_nl_mean##i.landed c.evap_min##i.landed c.ln_pop##i.landed ports ln_ship_mean milbase_total _fao_area* (ln_iuu_tot = ln_npp_tot) if to_use_panel == 1 & to_keep3 == 1 & year_bin==2005 & country_eez==1, dvarlag(W250n) force nolog impower(1)
est store sar_2005_I_im1
eststo, title("IV; 2005; EEZs")

spivregress pirates fragile c.ln_nl_mean##i.landed c.evap_min##i.landed c.ln_pop##i.landed ports ln_ship_mean milbase_total _fao_area* (ln_iuu_tot = ln_npp_tot) if to_use_panel == 1 & to_keep3 == 1 & year_bin==2010 & country_eez==1, dvarlag(W250n) force nolog impower(1)
est store sar_2010_I_im1
eststo, title("IV; 2010; EEZs")

spivregress pirates fragile c.ln_nl_mean##i.landed c.evap_min##i.landed c.ln_pop##i.landed ports ln_ship_mean milbase_total _fao_area* (ln_iuu_tot = ln_npp_tot) if to_use_panel == 1 & to_keep3 == 1 & year_bin==2005 & fao_area!=51, dvarlag(W250n) force nolog impower(1)
est store sar_2005_51_im1
eststo, title("IV; 2005; without FAO 51")

spivregress pirates fragile c.ln_nl_mean##i.landed c.evap_min##i.landed c.ln_pop##i.landed ports ln_ship_mean milbase_total _fao_area* (ln_iuu_tot = ln_npp_tot) if to_use_panel == 1 & to_keep3 == 1 & year_bin==2010 & fao_area!=51, dvarlag(W250n) force nolog impower(1)
est store sar_2010_51_im1
eststo, title("IV; 2010; without FAO 51")

spivregress pirates fragile c.ln_nl_mean##i.landed c.evap_min##i.landed c.ln_pop##i.landed ports ln_ship_mean milbase_total _fao_area* (ln_iuu_tot = ln_npp_tot) if to_use_panel == 1 & to_keep3 == 1 & year_bin==2005 & fao_area!=51 & country_eez==1, dvarlag(W250n) force nolog impower(1)
est store sar_2005_51_im1
eststo, title("IV; 2005; without FAO 51")

spivregress pirates fragile c.ln_nl_mean##i.landed c.evap_min##i.landed c.ln_pop##i.landed ports ln_ship_mean milbase_total _fao_area* (ln_iuu_tot = ln_npp_tot) if to_use_panel == 1 & to_keep3 == 1 & year_bin==2010 & fao_area!=51 & country_eez==1, dvarlag(W250n) force nolog impower(1)
est store sar_2010_51_im1
eststo, title("IV; 2010; without FAO 51")

esttab using "Tables/Table SI 6.csv", cells(b(star fmt(3)) se(fmt(3))) mlabels(, titles) stats(N) parentheses


